package com.team.web;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.team.dao.GoalDao;
import com.team.entity.Goal;
import com.team.entity.Project;
import com.team.entity.User;
import com.team.util.Const;
import com.team.util.StringUtil;
import com.team.util.TUtil;

public class AddGoalServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method = request.getParameter("_m");
		if("save".equals(method)){
			saveGoal(request,response);
		}else if("select".equals(method)){
			selectAllGoals(request,response);
		}else if("edit".equals(method)){
			selectGoal(request,response);
		}else if("update".equals(method)){
			updateGoal(request,response);
		}else if("find".equals(method)){
			findGoal(request,response);
			
		}else {
			response.sendError(404,"request a error path");
			return ;
		}
		
	}
	private void findGoal(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String goalId = request.getParameter("id");
		int id = Integer.parseInt(goalId);
		
		GoalDao gd = new GoalDao();
		
		Goal goal = gd.selectGoalById(id);
		if(goal != null){
			HttpSession session = request.getSession() ;
			session.setAttribute(Const.SESSION_SAVEGOAL, goal);
			request.getRequestDispatcher("/WEB-INF/views/task.jsp").forward(request, response);
			//response.sendRedirect("task.jsp");
			return ;
		}else {System.out.println("goal find error");}
		
	}
	private void updateGoal(HttpServletRequest request,
			HttpServletResponse response)throws ServletException, IOException {
		String  goalId= request.getParameter("id");
		int  id = Integer.parseInt(goalId);
		String name = request.getParameter("name");
		String content = request.getParameter("content");
		
		if(StringUtil.isEmpty(name)||StringUtil.isEmpty(content)){
			request.getRequestDispatcher("/WEB-INF/views/editgoal.jsp?code=1001").forward(request, response);
			//response.sendRedirect("editgoal.jsp?code=1001");
			return ;
		}
		GoalDao gd = new GoalDao();
		int num = gd.updateGoal(name,content,id);
		if(num > 0){
			
			response.sendRedirect("addGoal.jspx?_m=select");
			return ;
		}else {
			
			System.out.println("update goal error");
		}
		
	}
	private void selectGoal(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String goalId =request.getParameter("id");
		int  id = Integer.parseInt(goalId);
		GoalDao gd = new GoalDao();
		Goal goal = gd.selectGoalById(id);
		if(goal != null){
			HttpSession session = request.getSession();
			session.setAttribute(Const.SESSION_GOAL, goal);
			request.getRequestDispatcher("/WEB-INF/views/editgoal.jsp").forward(request, response);
			//response.sendRedirect("editgoal.jsp");
			return ;
		}
		
	}
	
	
	private void selectAllGoals(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
	//鏌ユ壘灞炰簬杩欎釜椤圭洰鐨勬墍鏈塯oals
		HttpSession session = request.getSession();
		Project project = (Project) session.getAttribute(Const.SESSION_PROJ);
		GoalDao gd = new GoalDao();
		List<Goal> goalList = gd.selectGoalsByProjId(project);
		if(goalList != null){
			System.out.println("杩欎釜椤圭洰鐨勬墍鏈塯oals 鎵惧埌浜�");
			for(Goal goal : goalList){
				System.out.println("goalId鈫�+goal.getId()"+"\tgoalname"+goal.getGoalName());
			}
			session.setAttribute(Const.SESSION_GOALS,goalList );
		}
		request.getRequestDispatcher("/WEB-INF/views/goal.jsp").forward(request, response);
		//response.sendRedirect("goal.jsp");
		return ;
	}
	private void saveGoal(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException  {
		String name = request.getParameter("name");
		String content = request.getParameter("content");
		
		if(StringUtil.isEmpty(name)||StringUtil.isEmpty(content)){
			request.getRequestDispatcher("/WEB-INF/views/editgoal.jsp?code=1001").forward(request, response);
			//response.sendRedirect("editgoal.jsp?code=1001");
			return ;
		}
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute(Const.SESSION_USER);
		Project project = (Project) session.getAttribute(Const.SESSION_PROJ);
		int projId = project.getId();
		
		GoalDao gd = new GoalDao();
		Goal goal = new Goal();
		
		goal.setGoalName(name);
		goal.setContent(content);
		goal.setCreateTime(TUtil.getNow());
		goal.setUserId(user.getId());
		goal.setProjId(projId);
		
		int num = gd.saveGoal(goal);
		if(num >0 ){
			System.out.println("goal save succ");
			response.sendRedirect("addGoal.jspx?_m=select");
			return ;
		}else {
			System.out.println("goal save fail");
			request.getRequestDispatcher("/WEB-INF/views/goal.jsp?code=1001").forward(request, response);
			//response.sendRedirect("goal.jsp?code=1001");
			return ;
		}
		
	}
}
